<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Docker常用命令 | 邢昊波的个人笔记</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/favicon.ico">
    <meta name="description" content="记录生活，记录点滴">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.d32511b8.css" as="style"><link rel="preload" href="/assets/js/app.44c52f9e.js" as="script"><link rel="preload" href="/assets/js/3.60b348b7.js" as="script"><link rel="preload" href="/assets/js/1.576b739e.js" as="script"><link rel="preload" href="/assets/js/10.36119258.js" as="script"><link rel="prefetch" href="/assets/js/11.7184074e.js"><link rel="prefetch" href="/assets/js/12.3e7b79bc.js"><link rel="prefetch" href="/assets/js/13.35e27885.js"><link rel="prefetch" href="/assets/js/14.60e16f38.js"><link rel="prefetch" href="/assets/js/15.6cd1409a.js"><link rel="prefetch" href="/assets/js/16.b70ca709.js"><link rel="prefetch" href="/assets/js/4.00518278.js"><link rel="prefetch" href="/assets/js/5.ecf37b4b.js"><link rel="prefetch" href="/assets/js/6.fa756a2e.js"><link rel="prefetch" href="/assets/js/7.b13a6b81.js"><link rel="prefetch" href="/assets/js/8.93d9d6ae.js"><link rel="prefetch" href="/assets/js/9.87fd42a4.js">
    <link rel="stylesheet" href="/assets/css/0.styles.d32511b8.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-1aefc0b4><div data-v-1aefc0b4><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-1aefc0b4 data-v-1aefc0b4><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-1aefc0b4 data-v-1aefc0b4><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>邢昊波的个人笔记</h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2>记录生活，记录点滴</p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>xinghb</span>
            
          <span data-v-25ba6db2>2021-2022 - </span>
          2022
        </a></span></div></div> <div class="hide" data-v-1aefc0b4><header class="navbar" data-v-1aefc0b4><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="邢昊波的个人笔记" class="logo"> <span class="site-name">邢昊波的个人笔记</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      服务部署
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/docs/linux/" class="nav-link"><i class="undefined"></i>
  Linux相关
</a></li><li class="dropdown-item"><!----> <a href="/docs/docker/" aria-current="page" class="nav-link router-link-exact-active router-link-active"><i class="undefined"></i>
  Docker相关
</a></li></ul></div></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  归档
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/devinbo" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-1aefc0b4></div> <aside class="sidebar" data-v-1aefc0b4><div class="personal-info-wrapper" data-v-39576ba9 data-v-1aefc0b4><img src="/avatar.png" alt="author-avatar" class="personal-img" data-v-39576ba9> <h3 class="name" data-v-39576ba9>
    xinghb
  </h3> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>6</h3> <h6 data-v-39576ba9>Articles</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>3</h3> <h6 data-v-39576ba9>Tags</h6></div></div> <ul class="social-links" data-v-39576ba9></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      服务部署
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/docs/linux/" class="nav-link"><i class="undefined"></i>
  Linux相关
</a></li><li class="dropdown-item"><!----> <a href="/docs/docker/" aria-current="page" class="nav-link router-link-exact-active router-link-active"><i class="undefined"></i>
  Docker相关
</a></li></ul></div></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  归档
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/devinbo" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <ul class="sidebar-links"><li><a href="/docs/docker/" aria-current="page" class="active sidebar-link">Docker常用命令</a></li><li><a href="/docs/docker/rocketMq.html" class="sidebar-link">Docker安装RocketMq</a></li><li><a href="/docs/docker/nginx.html" class="sidebar-link">Docker安装Nginx</a></li><li><a href="/docs/docker/plugin.html" class="sidebar-link">plugin</a></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-1aefc0b4><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>Docker常用命令</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>xinghb</span>
            
          <span data-v-25ba6db2>2021-2022 - </span>
          2022
        </a></span></div></div> <div data-v-1aefc0b4><main class="page"><section><div class="page-title"><h1 class="title">Docker常用命令</h1> <div data-v-f875f3fc><i class="iconfont reco-account" data-v-f875f3fc><span data-v-f875f3fc>xinghb</span></i> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>1/22/2022</span></i> <!----> <i class="tags iconfont reco-tag" data-v-f875f3fc><span class="tag-item" data-v-f875f3fc>docker</span></i></div></div> <div class="theme-reco-content content__default"><div class="custom-block tip"><p class="title"></p><p>平时经常使用Docker来搭建各种环境，简单又好用！但是有时候往往会忘记命令，总结了一套非常实用的Docker命令，对于Java开发来说基本上够用了，希望对大家有所帮助！</p></div><h2 id="docker环境安装"><a href="#docker环境安装" class="header-anchor">#</a> docker环境安装</h2> <ul><li>安装<code>yum-utils</code>；</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>yum <span class="token function">install</span> -y yum-utils device-mapper-persistent-data lvm2
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>为yum源添加docker仓库位置；</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>安装docker服务；</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>yum <span class="token function">install</span> docker-ce
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>启动docker服务。</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>systemctl start <span class="token function">docker</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="docker镜像常用命令"><a href="#docker镜像常用命令" class="header-anchor">#</a> Docker镜像常用命令</h2> <h3 id="搜索镜像"><a href="#搜索镜像" class="header-anchor">#</a> 搜索镜像</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> search java
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="下载镜像"><a href="#下载镜像" class="header-anchor">#</a> 下载镜像</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> pull java:8
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="查看镜像版本"><a href="#查看镜像版本" class="header-anchor">#</a> 查看镜像版本</h3> <div class="custom-block warning"><p class="title"></p><p>由于<code>docker search</code>命令只能查找出是否有该镜像，不能找到该镜像支持的版本，所以我们需要通过<code>Docker Hub</code>来搜索支持的版本。</p></div><ul><li>进入<code>Docker Hub</code>的官网，地址：<a href="https://hub.docker.com/" target="_blank" rel="noopener noreferrer">https://hub.docker.com<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li>然后搜索需要的镜像：</li></ul> <p><img src="/assets/img/docker_command_02.7400900d.png" alt="docker_command_02"></p> <ul><li>查看镜像支持的版本：</li></ul> <p><img src="/assets/img/docker_command_03.80ca1a87.png" alt="img"></p> <ul><li>进行镜像的下载操作：</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> pull nginx:1.17.0
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="列出镜像"><a href="#列出镜像" class="header-anchor">#</a> 列出镜像</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> images
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="删除镜像"><a href="#删除镜像" class="header-anchor">#</a> 删除镜像</h3> <ul><li>指定名称删除镜像：</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> rmi java:8
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>指定名称删除镜像（强制）：</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> rmi -f java:8
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>删除所有没有引用的镜像：</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> rmi <span class="token variable"><span class="token variable">`</span><span class="token function">docker</span> images <span class="token operator">|</span> <span class="token function">grep</span> none <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'{print $3}'</span><span class="token variable">`</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>强制删除所有镜像：</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> rmi <span class="token variable"><span class="token variable">`</span><span class="token function">docker</span> images <span class="token operator">|</span> <span class="token function">grep</span> none <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'{print $3}'</span><span class="token variable">`</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="打包镜像"><a href="#打包镜像" class="header-anchor">#</a> 打包镜像</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token comment"># -t 表示指定镜像仓库名称/镜像名称:镜像标签 .表示使用当前目录下的Dockerfile文件</span>
<span class="token function">docker</span> build -t mall/mall-admin:1.0-SNAPSHOT <span class="token builtin class-name">.</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="推送镜像"><a href="#推送镜像" class="header-anchor">#</a> 推送镜像</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token comment"># 登录Docker Hub</span>
<span class="token function">docker</span> login
<span class="token comment"># 给本地镜像打标签为远程仓库名称</span>
<span class="token function">docker</span> tag mall/mall-admin:1.0-SNAPSHOT macrodocker/mall-admin:1.0-SNAPSHOT
<span class="token comment"># 推送到远程仓库</span>
<span class="token function">docker</span> push macrodocker/mall-admin:1.0-SNAPSHOT

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h2 id="docker容器常用命令"><a href="#docker容器常用命令" class="header-anchor">#</a> Docker容器常用命令</h2> <h3 id="新建并启动容器"><a href="#新建并启动容器" class="header-anchor">#</a> 新建并启动容器</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> run -p <span class="token number">80</span>:80 --name nginx <span class="token punctuation">\</span>
-e <span class="token assign-left variable">TZ</span><span class="token operator">=</span><span class="token string">&quot;Asia/Shanghai&quot;</span> <span class="token punctuation">\</span>
-v /mydata/nginx/html:/usr/share/nginx/html <span class="token punctuation">\</span>
-d nginx:1.17.0
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><ul><li>-p：将宿主机和容器端口进行映射，格式为：宿主机端口:容器端口；</li> <li>--name：指定容器名称，之后可以通过容器名称来操作容器；</li> <li>-e：设置容器的环境变量，这里设置的是时区；</li> <li>-v：将宿主机上的文件挂载到宿主机上，格式为：宿主机文件目录:容器文件目录；</li> <li>-d：表示容器以后台方式运行。</li></ul> <h3 id="列出容器"><a href="#列出容器" class="header-anchor">#</a> 列出容器</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token comment">#列出运行中的容器</span>
<span class="token function">docker</span> <span class="token function">ps</span>
<span class="token comment">#列出所有容器</span>
<span class="token function">docker</span> <span class="token function">ps</span> -a
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="停止容器"><a href="#停止容器" class="header-anchor">#</a> 停止容器</h3> <p>注意：<code>$ContainerName</code>表示容器名称，<code>$ContainerId</code>表示容器ID，可以使用容器名称的命令，基本也支持使用容器ID，比如下面的停止容器命令。</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> stop <span class="token variable">$ContainerName</span><span class="token punctuation">(</span>or <span class="token variable">$ContainerId</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="强制停止容器"><a href="#强制停止容器" class="header-anchor">#</a> 强制停止容器</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> <span class="token function">kill</span> <span class="token variable">$ContainerName</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="启动容器"><a href="#启动容器" class="header-anchor">#</a> 启动容器</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> start <span class="token variable">$ContainerName</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="进入容器"><a href="#进入容器" class="header-anchor">#</a> 进入容器</h3> <ul><li>先查询出容器的<code>pid</code>：</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> inspect --format <span class="token string">&quot;{{.State.Pid}}&quot;</span> <span class="token variable">$ContainerName</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>根据容器的pid进入容器：</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>nsenter --target <span class="token string">&quot;<span class="token variable">$pid</span>&quot;</span> --mount --uts --ipc --net --pid
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="删除容器"><a href="#删除容器" class="header-anchor">#</a> 删除容器</h3> <ul><li>删除指定容器：</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> <span class="token function">rm</span> <span class="token variable">$ContainerName</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>按名称通配符删除容器，比如删除以名称<code>mall-</code>开头的容器：</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> <span class="token function">rm</span> <span class="token variable"><span class="token variable">`</span><span class="token function">docker</span> <span class="token function">ps</span> -a <span class="token operator">|</span> <span class="token function">grep</span> mall-* <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'{print $1}'</span><span class="token variable">`</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>强制删除所有容器；</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> <span class="token function">rm</span> -f <span class="token variable"><span class="token variable">$(</span><span class="token function">docker</span> <span class="token function">ps</span> -a -q<span class="token variable">)</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="查看容器的日志"><a href="#查看容器的日志" class="header-anchor">#</a> 查看容器的日志</h3> <ul><li>查看容器产生的全部日志：</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> logs <span class="token variable">$ContainerName</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>动态查看容器产生的日志：</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> logs -f <span class="token variable">$ContainerName</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="查看容器的ip地址"><a href="#查看容器的ip地址" class="header-anchor">#</a> 查看容器的ip地址</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> inspect --format <span class="token string">'{{ .NetworkSettings.IPAddress }}'</span> <span class="token variable">$ContainerName</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="修改容器启动方式"><a href="#修改容器启动方式" class="header-anchor">#</a> 修改容器启动方式</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token comment"># 将容器启动方式改为always</span>
<span class="token function">docker</span> container update --restart<span class="token operator">=</span>always <span class="token variable">$ContainerName</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="同步宿主机时间到容器"><a href="#同步宿主机时间到容器" class="header-anchor">#</a> 同步宿主机时间到容器</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> <span class="token function">cp</span> /etc/localtime <span class="token variable">$ContainerName</span>:/etc/
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="指定容器时区"><a href="#指定容器时区" class="header-anchor">#</a> 指定容器时区</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> run -p <span class="token number">80</span>:80 --name nginx <span class="token punctuation">\</span>
-e <span class="token assign-left variable">TZ</span><span class="token operator">=</span><span class="token string">&quot;Asia/Shanghai&quot;</span> <span class="token punctuation">\</span>
-d nginx:1.17.0
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="查看容器资源占用情况"><a href="#查看容器资源占用情况" class="header-anchor">#</a> 查看容器资源占用情况</h3> <ul><li>查看指定容器资源占用状况，比如cpu、内存、网络、io状态：</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> stats <span class="token variable">$ContainerName</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>查看所有容器资源占用情况：</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> stats -a
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="查看容器磁盘使用情况"><a href="#查看容器磁盘使用情况" class="header-anchor">#</a> 查看容器磁盘使用情况</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> system <span class="token function">df</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="执行容器内部命令"><a href="#执行容器内部命令" class="header-anchor">#</a> 执行容器内部命令</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> <span class="token builtin class-name">exec</span> -it <span class="token variable">$ContainerName</span> /bin/bash
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="指定账号进入容器内部"><a href="#指定账号进入容器内部" class="header-anchor">#</a> 指定账号进入容器内部</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token comment"># 使用root账号进入容器内部</span>
<span class="token function">docker</span> <span class="token builtin class-name">exec</span> -it --user root <span class="token variable">$ContainerName</span> /bin/bash
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="查看所有网络"><a href="#查看所有网络" class="header-anchor">#</a> 查看所有网络</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> network <span class="token function">ls</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="创建外部网络"><a href="#创建外部网络" class="header-anchor">#</a> 创建外部网络</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> network create -d bridge my-bridge-network
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="指定容器网络"><a href="#指定容器网络" class="header-anchor">#</a> 指定容器网络</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> run -p <span class="token number">80</span>:80 --name nginx <span class="token punctuation">\</span>
--network my-bridge-network <span class="token punctuation">\</span>
-d nginx:1.17.0
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="修改镜像存放位置"><a href="#修改镜像存放位置" class="header-anchor">#</a> 修改镜像存放位置</h2> <ul><li>查看Docker镜像的存放位置：</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> info <span class="token operator">|</span> <span class="token function">grep</span> <span class="token string">&quot;Docker Root Dir&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>关闭Docker服务：</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>systemctl stop <span class="token function">docker</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>先将原镜像目录移动到目标目录：</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">mv</span> /var/lib/docker /mydata/docker
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>建立软连接：</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">ln</span> -s /mydata/docker /var/lib/docker
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>再次查看可以发现镜像存放位置已经更改。</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> info <span class="token operator">|</span> <span class="token function">grep</span> <span class="token string">&quot;Docker Root Dir&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="docker容器清理"><a href="#docker容器清理" class="header-anchor">#</a> Docker容器清理</h2> <ul><li>查看Docker占用的磁盘空间情况：</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> system <span class="token function">df</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>删除所有关闭的容器：</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> <span class="token function">ps</span> -a <span class="token operator">|</span> <span class="token function">grep</span> Exit <span class="token operator">|</span> <span class="token function">cut</span> -d <span class="token string">' '</span> -f <span class="token number">1</span> <span class="token operator">|</span> <span class="token function">xargs</span> <span class="token function">docker</span> <span class="token function">rm</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>删除所有<code>dangling</code>镜像(没有Tag的镜像)：</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> rmi <span class="token variable"><span class="token variable">$(</span><span class="token function">docker</span> images <span class="token operator">|</span> <span class="token function">grep</span> <span class="token string">&quot;^&lt;none&gt;&quot;</span> <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">&quot;{print <span class="token variable">$3</span>}&quot;</span><span class="token variable">)</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>删除所有<code>dangling</code>数据卷(即无用的 volume)：</li></ul> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">docker</span> volume <span class="token function">rm</span> <span class="token variable"><span class="token variable">$(</span><span class="token function">docker</span> volume <span class="token function">ls</span> -qf <span class="token assign-left variable">dangling</span><span class="token operator">=</span>true<span class="token variable">)</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></div></section> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><!----> <span class="next"><a href="/docs/docker/rocketMq.html">
            Docker安装RocketMq
          </a></span></p></div> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-cb1513f6><li class="level-2" data-v-cb1513f6><a href="/docs/docker/#docker环境安装" class="sidebar-link reco-side-docker环境安装" data-v-cb1513f6>docker环境安装</a></li><li class="level-2" data-v-cb1513f6><a href="/docs/docker/#docker镜像常用命令" class="sidebar-link reco-side-docker镜像常用命令" data-v-cb1513f6>Docker镜像常用命令</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#搜索镜像" class="sidebar-link reco-side-搜索镜像" data-v-cb1513f6>搜索镜像</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#下载镜像" class="sidebar-link reco-side-下载镜像" data-v-cb1513f6>下载镜像</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#查看镜像版本" class="sidebar-link reco-side-查看镜像版本" data-v-cb1513f6>查看镜像版本</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#列出镜像" class="sidebar-link reco-side-列出镜像" data-v-cb1513f6>列出镜像</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#删除镜像" class="sidebar-link reco-side-删除镜像" data-v-cb1513f6>删除镜像</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#打包镜像" class="sidebar-link reco-side-打包镜像" data-v-cb1513f6>打包镜像</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#推送镜像" class="sidebar-link reco-side-推送镜像" data-v-cb1513f6>推送镜像</a></li><li class="level-2" data-v-cb1513f6><a href="/docs/docker/#docker容器常用命令" class="sidebar-link reco-side-docker容器常用命令" data-v-cb1513f6>Docker容器常用命令</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#新建并启动容器" class="sidebar-link reco-side-新建并启动容器" data-v-cb1513f6>新建并启动容器</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#列出容器" class="sidebar-link reco-side-列出容器" data-v-cb1513f6>列出容器</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#停止容器" class="sidebar-link reco-side-停止容器" data-v-cb1513f6>停止容器</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#强制停止容器" class="sidebar-link reco-side-强制停止容器" data-v-cb1513f6>强制停止容器</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#启动容器" class="sidebar-link reco-side-启动容器" data-v-cb1513f6>启动容器</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#进入容器" class="sidebar-link reco-side-进入容器" data-v-cb1513f6>进入容器</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#删除容器" class="sidebar-link reco-side-删除容器" data-v-cb1513f6>删除容器</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#查看容器的日志" class="sidebar-link reco-side-查看容器的日志" data-v-cb1513f6>查看容器的日志</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#查看容器的ip地址" class="sidebar-link reco-side-查看容器的ip地址" data-v-cb1513f6>查看容器的ip地址</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#修改容器启动方式" class="sidebar-link reco-side-修改容器启动方式" data-v-cb1513f6>修改容器启动方式</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#同步宿主机时间到容器" class="sidebar-link reco-side-同步宿主机时间到容器" data-v-cb1513f6>同步宿主机时间到容器</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#指定容器时区" class="sidebar-link reco-side-指定容器时区" data-v-cb1513f6>指定容器时区</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#查看容器资源占用情况" class="sidebar-link reco-side-查看容器资源占用情况" data-v-cb1513f6>查看容器资源占用情况</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#查看容器磁盘使用情况" class="sidebar-link reco-side-查看容器磁盘使用情况" data-v-cb1513f6>查看容器磁盘使用情况</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#执行容器内部命令" class="sidebar-link reco-side-执行容器内部命令" data-v-cb1513f6>执行容器内部命令</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#指定账号进入容器内部" class="sidebar-link reco-side-指定账号进入容器内部" data-v-cb1513f6>指定账号进入容器内部</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#查看所有网络" class="sidebar-link reco-side-查看所有网络" data-v-cb1513f6>查看所有网络</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#创建外部网络" class="sidebar-link reco-side-创建外部网络" data-v-cb1513f6>创建外部网络</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/docker/#指定容器网络" class="sidebar-link reco-side-指定容器网络" data-v-cb1513f6>指定容器网络</a></li><li class="level-2" data-v-cb1513f6><a href="/docs/docker/#修改镜像存放位置" class="sidebar-link reco-side-修改镜像存放位置" data-v-cb1513f6>修改镜像存放位置</a></li><li class="level-2" data-v-cb1513f6><a href="/docs/docker/#docker容器清理" class="sidebar-link reco-side-docker容器清理" data-v-cb1513f6>Docker容器清理</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/assets/js/app.44c52f9e.js" defer></script><script src="/assets/js/3.60b348b7.js" defer></script><script src="/assets/js/1.576b739e.js" defer></script><script src="/assets/js/10.36119258.js" defer></script>
  </body>
</html>
